package org.myspringframework.aop.framework.autoproxy;

import org.myspringframework.aop.Advisor;

import java.util.List;

//能够识别加了@Aspect注解标记的bean并转换为advisor
public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator{

    private BeanFactoryAspectJAdvisorsBuilder aspectJAdvisorsBuilder=new BeanFactoryAspectJAdvisorsBuilder();
    //用于防止多次扫描@Aspect注解
    boolean isInit=false;

    //获取所有advisor,包括xml自定义的和@Aspect注解标识的
    @Override
    protected List<Advisor> findCandidateAdvisors() {
        //调用父类的方法，获取容器中所有Advisor类型的bean
        List<Advisor> advisors = super.findCandidateAdvisors();

        if (this.aspectJAdvisorsBuilder != null&& !isInit) {
            isInit=true;
            aspectJAdvisorsBuilder.setBeanFactory(getBeanFactory());
            // 获取被注解@Aspect标识的Bean 并将其转换为多个advisor低级切面
            advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
        }
        return advisors;
    }

    //获取提前暴露的对象
    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) {
        //放入提前代理的map中
        earlyProxyReferences.put(beanName,bean);
        //如果需要代理，则提前进行aop代理
        Object proxy = this.wrapIfNecessary(bean, beanName);

        return proxy;
    }

}
